<template>
	<view>
		<view class="userInfo">
			<view class="top">
				<image src="../../static/西瓜.png" mode="widthFix"></image>
				<text>{{form.username}}</text>
			</view>
			<view class="bottom">
				<uni-forms ref="form" :model="form" :rules="rules">
					<uni-forms-item required name="nickName" label="昵称">
						<uni-easyinput v-model="form.nickName" type="text" placeholder="请输入昵称" />
					</uni-forms-item>
					<uni-forms-item name="username" label="用户名">
						<uni-easyinput required v-model="form.username" disabled type="text" />
					</uni-forms-item>
					<uni-forms-item required name="password" label="密码">
						<uni-easyinput v-model="form.password" type="password" placeholder="请输入密码" />
					</uni-forms-item>
				</uni-forms>
				<van-button @click="save" round type="info" color="#38A678" size="large">保存</van-button>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	export default {
		data() {
			return {
				form: {
					username: "",
					password: "",
					nickName:""
				},
				rules: {
					password: {
						rules: [{
								required: true,
								errorMessage: '请输入密码',
							},
							{
								minLength: 6,
								maxLength: 12,
								errorMessage: '密码长度在 {minLength} 到 {maxLength} 个字符',
							}
						]
					},
				}
			};
		},
		computed: {
			...mapState('m_user', ['userInfo']),
		},
		onLoad() {
			this.form.username = this.userInfo.username
			this.form.password = this.userInfo.password
			this.form.nickName = this.userInfo.nickName
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo']),
			// 保存方法
			save() {
				this.$refs.form.validate().then(res => {
					console.log('表单数据信息：', res);
					this.saveInfo(res)
					// 跳转页面
					setTimeout(()=>{
						uni.switchTab({
							url: '/pages/my/my'
						});
					},2000)
				}).catch(err => {
					console.log('表单错误信息：', err);
				})
			},
			async saveInfo(res) {
				const {
					data
				} = await uni.$http.put("/user/updateUser", res)
				console.log('保存密码', data);
				this.updateUserInfo(res);
				uni.$showMsg("修改信息成功")
			}

		}
	}
</script>

<style lang="scss">
	.userInfo {
		height: 100vh;

		.top {
			height: 200px;
			background-color: #38A678;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;

			image {
				width: 100px;
			}
		}

		.bottom {
			box-sizing: border-box;
			padding: 20px;
			height: calc(100vh - 200px);
			background-color: #fff;
		}
	}
</style>
